package com.example.demo53.util.Adapter;

import static com.example.color3.colorpicker.ColorPicker.blue;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.demo53.R;

import java.util.List;

public class GridViewAdapter extends BaseAdapter {
    private Context context;
    private List<Integer> img_list;
    private LayoutInflater layoutInflater;
    private List<String> tv_list;

    private ImageView mImageView;
    private TextView mTextview;
    private int clickTemp = -1;
    private int g_selectItem_state = 0;
    public GridViewAdapter(Context context,List<Integer> img_list,List<String> tv_list) {
        this.context = context;
        this.tv_list = tv_list;
        this.img_list= img_list;
        layoutInflater = LayoutInflater.from(context);
       // icons = Icon;
    }

    //标识选择的Item位置
    public void setSeclection(int position) {
        clickTemp = position;
    }

    public int getg_selectItem_state(){
        return g_selectItem_state;
    }
    //判断g_gridview是否开启选择模式（设置组的时候
    //1：开启 0：关闭
    public void setg_selectItem_state(int num) {
        g_selectItem_state = num;
    }

    @Override
    public int getCount() {
        return img_list.size();//注意此处
    }

    @Override
    public Object getItem(int position) {
        return img_list.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = layoutInflater.inflate(R.layout.gv_item1, null);
        mImageView = (ImageView) convertView.findViewById(R.id.img_gvitem);
        mTextview = convertView.findViewById(R.id.tv_gvitem);
//-------------------不判断就报错
        if (position <img_list.size()) {
            mImageView.setBackgroundResource(img_list.get(position));
            mTextview.setText(tv_list.get(position));

        }
        if(g_selectItem_state==1) {
            if (clickTemp == position) {
                convertView.setBackgroundResource(R.drawable.g_select_bg);
            } else {
                convertView.setBackgroundColor(blue);
            }
        }
        return convertView;

    }


}

